﻿using AutoMapper;
using BestTrader.Cms.RmmfSoftServices.Models;
using System;
using System.Text;

namespace BestTrader.Cms.Admin.Profiles
{
    public abstract class CustomerResolver<T> : ITypeConverter<T, CustomerInput>
    {
        #region 1. 随机生成11位手机号码
        /// <summary>
        /// 随机生成11位手机号码
        /// </summary>
        public String GetRandomPhone()
        {
            String[] 移动 = { "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "187", "188" };
            String[] 联通 = { "130", "131", "132", "155", "156", "185", "186", "145" };
            String[] 电信 = { "133", "153", "180", "181", "189" };
            String[][] tel = { 移动, 联通, 电信 };

            Random rd = new Random();
            int index = rd.Next(tel.Length);        // 运营商个数
            int size = rd.Next(tel[index].Length);  // 各运营商号码个数
            String phone = tel[index][size] + "" + rd.Next(10) + "" + rd.Next(10) + "" + rd.Next(10) + "" + rd.Next(10) + "" + rd.Next(10) + "" + rd.Next(10) + "" + rd.Next(10) + "" + rd.Next(10);
            return phone;
        } 
        #endregion

        #region 2. 随机生成身份证号码
        //生成随机年份
        static int generateYear()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            int year = rd.Next(1900, DateTime.Now.Year);
            bu.Append(year);
            return int.Parse(bu.ToString());
        }

        //生成3位随机
        static string randomcode()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            int code = rd.Next(1, 999);
            if (code < 10)
            {
                bu.Append(00);
            }
            else if (code < 100)
            {
                bu.Append(0);
            }
            bu.Append(code);
            return bu.ToString();
        }


        static bool isleapyear(int year)
        {
            if (year % 4 == 0)
            {
                if (year % 100 == 0 && year % 400 != 0)
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 随机生成身份证号码
        /// </summary>
        /// <returns></returns>
        public String GetRandomCardId()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            bu.Append(110000);
            int year = generateYear();
            bu.Append(year);
            int month = rd.Next(1, 12);
            if (month < 10)
            {
                bu.Append(0);
            }
            bu.Append(month);
            int[] days;
            if (isleapyear(year))
            {
                days = new int[12] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }
            else
            {
                days = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }

            int day = rd.Next(1, days[month]);
            if (day < 10)
            {
                bu.Append(0);
            }
            bu.Append(day);
            bu.Append(randomcode());
            int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            string convert = bu.ToString();

            int it = 0;
            int res = 0;
            //  Console.WriteLine(res);

            while (it < 17)
            {
                res = res + (convert[it] - '0') * c[it];
                //   Console.WriteLine("第"+it+"次,"+convert[it]+"乘以"+c[it]);
                //    Console.WriteLine(res);
                it++;
            }


            int i = res % 11;
            bu.Append(r[i]);
            return bu.ToString();
        }
        #endregion

        #region 3. 随机生成银行卡

        /// <summary>
        /// 随机生成银行卡
        /// </summary>
        /// <param name="len"></param>
        public string GetRandomBankAccount(int len = 20)
        {
            string card = string.Empty;
            if (len > 0)
            {
                string _chars = "0123456789";
                int max = _chars.Length;
                Random ran = new Random();
                for (int i = 0; i < len; i++)
                {
                    card += _chars[ran.Next(max)];
                }
            }
            return card;
        }
        #endregion

        #region 4. 随机生成邮箱

        /// <summary>
        /// 随机生成邮箱
        /// </summary>
        /// <returns></returns>
        public string GetRandomEmail() {
            string email = GetRandomBankAccount(10);
            string[] net = { "126", "qq", "163", "gmail", "sina", "baidu", "117", "136" };
            Random ran = new Random();
            return $"{email}@{net[ran.Next(net.Length - 1)]}.com";
        }
        #endregion

        public abstract CustomerInput Convert(T source, CustomerInput destination, ResolutionContext context);
    }
}
